package utils;

import java.util.Arrays;
import java.util.List;
import java.util.function.*;



/**
 * 使用lambda表达式和函数式接口Predicate
 */
public class App4 {

    public static void main(String[] args) {
        List<String> list = Arrays.asList("java", "scala", "c++", "haskhell", "lisp");
        filter(list, str -> ((String)str).startsWith("j"));
        filter(list, str -> ((String)str).length() > 4);

        // 甚至可以用and()、or()和xor()逻辑函数来合并Predicate，
        // 例如要找到所有以J开始，长度为四个字母的名字，你可以合并两个Predicate并传入
        Predicate<String> startsWithJ = (n) -> n.startsWith("j");
        Predicate<String> fourLetterLong = (n) -> n.length() == 4;
        list.stream()
                .filter(startsWithJ.and(fourLetterLong))
                .forEach(n -> System.out.println("过滤"+n));
    }

    public static void filter(List list,Predicate predicate){
        list.forEach(n -> {
            if(predicate.test(n)){//表达式是否满足
                System.out.print(n+" 1 ");
            }
        });
    }
}
